var config = require('./config.js');
var async = require('async');
var fs = require('fs');
var path = require('path');
var UUID = require('uuid');
var request = require('request');
// var dir = config.start + "到" + config.end;
var dir = "test";
var errs = [];
console.log(config.imgs)
var getImages = function(imgs, cb) {

    async.mapSeries(imgs, function(img, _cb) {
        var current = 0;
        var total = 0;
        if (!fs.existsSync(path.resolve(__dirname, "download", dir))) {
            fs.mkdir(path.resolve(__dirname, "download", dir));
        }
        var filename = UUID.v1();
        request.get(img, { timeout: config.timeout }, function(error) {
                _cb(null);

            })
            .on('response', function(res) {
                total = res.headers['content-length'];
            })
            .on('data', function(data) {
                current += data.length;
                console.log((current / total).toFixed(4) * 100 + "%");

            })
            .on('error', function() {
                if (fs.existsSync(path.resolve(__dirname, 'download/', dir, filename + '.jpg'))) {
                    fs.unlinkSync(path.resolve(__dirname, 'download/', dir, filename + '.jpg'));
                }
                errs.push(img);
            })
            .pipe(fs.createWriteStream(path.resolve(__dirname, 'download', dir, filename + '.jpg')));
    }, function() {
        if (errs.length === 0) {
            console.log("全部抓取成功")
        } else {
            console.log(errs)
        }
    });
}
getImages(config.imgs);